package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.CheckgroupDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckgroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

/**
 * 检查组业务层
 *
 * @author likeWen
 * @since 2022/11/19 11:37
 */
@Service
@Slf4j

public class CheckgroupServiceImpl implements CheckgroupService {
    @Autowired
    private CheckgroupDao checkgroupDao;

    /**
     * 检查项分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        log.info("[检查组-分页查询]data：{}",queryPageBean);
        //使用分页插件
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        // 分页查询数据库
        Page<CheckGroup> page = checkgroupDao.selectByConditon(queryPageBean.getQueryString());
        //封装分页数据
        return new PageResult(page.getTotal(),page.getResult());
    }
    /**
     * 检查项新增
     * @param
     */
    @Override
    @Transactional
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
        log.info("[检查组-新增]data:{},ids:{}",checkGroup,checkitemIds);
        //插入数据
        checkgroupDao.insert(checkGroup);
        checkgroupDao.insertCheckGroupAndCheckItem(checkGroup.getId(),checkitemIds);
    }
    @Override
    public CheckGroup findById(Integer id) {
        log.info("[检查组-根据ID查询]id:{}",id);
        return checkgroupDao.selectById(id);
    }

    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        log.info("[检查组-根据检查项ID查询]id:{}",id);
        return checkgroupDao.findCheckItemIdsByCheckGroupId(id);
    }

    @Override
    public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {
        log.info("[检查组-编辑检查组]data:{},checkitemIds:{}", checkGroup,checkitemIds);
        //更新检查组
        checkgroupDao.update(checkGroup);
        //根据检查组id 删除原有关系
        checkgroupDao.CheckGroupAndCheckItemById(checkGroup.getId());
        //重新加入关系
        checkgroupDao.insertCheckGroupAndCheckItem(checkGroup.getId(),checkitemIds);
    }

    @Override
    public Boolean deleteById(Integer id) {
        log.info("[检查组-根据ID删除]id:{}",id);
        //查询是否被套餐引用
        Integer count =checkgroupDao.selectBySetmeal(id);
        if (count == 0){
            //删除绑定关系
            checkgroupDao.CheckGroupAndCheckItemById(id);
            //实际删除操作
            checkgroupDao.deleteById(id);
            return true;
        }
        return false;
    }

    @Override
    public List<CheckGroup> findAll() {
        log.info("[套餐-查询检查组信息]data:{}");
        return checkgroupDao.findAll();
    }


}
